const Router = require('express').Router
const express = require('express')
const RepoController = require('../controller/repo')

const router = Router()

router.get('/repo/tree', async (req, res) => {
    const { username, repo, path } = req.query
    let branch = req.query.branch ? req.query.branch : 'master'
    const repoController = new RepoController
    const context = {
        user: null,
        repoItem: null,
        params: {
            username,
            repo,
            path,
            branch,
        },
        data: {
            isEmptyRepo: false, // 判断是不是空仓库（是否没有分支），在init中赋值
            branch: '',
            list: [],
            commit_set: [],
            branchArray: [],
            // 仓库分支的profile
            profile: {
                description: '',
                website: '',
            },
        }  
    }
    await repoController.init(context)
    if (!context.isEmptyRepo) { // 仓库不为空时
        await repoController.tree(context)
    }
    return res.send({
        code: 20000,
        data: context.data,
    })
})

router.get('/repo/blob', async (req, res) => {
    const { username, branch, repo, path } = req.query
    const repoController = new RepoController()
    const context = {
        user: null,
        repoItem: null,
        params: {
            username,
            repo,
            path,
            branch,
        },
        data: {
            branch: '',
            commit_set: [],
            branchArray: [],
            // 仓库分支的profile
            profile: {
                description: '',
                website: '',
            },

            codeLines: [],
        }  
    }
    await repoController.init(context)
    await repoController.blob(context)
    res.send({
        code: 20000,
        data: context.data,
    })
})

module.exports = router